import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'print'
  },
  {
    path: '/home',
    redirect: 'print',
    component: () => import('../views/Home'),
    children: [
      { path: '/print', component: () => import('../components/Print') },
      {
        path: '/me',
        redirect: '/info',
        component: () => import('../components/Me'),
        children: [
          { path: '/info', component: () => import('../components/Info') },
          { path: '/order', component: () => import('../components/Order') }
        ]
      }


    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login')
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('../views/Admin')
  },
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由卫士
router.beforeEach((to, from, next) => {
  if (to.name !== 'Login' && !localStorage.getItem("uid")) next({ name: 'Login' })
  else next()
})

export default router
